<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/18
 * Time: 3:23 PM
 */
namespace app\admin\controller;


use app\admin\logic\CleaningPointLogic;
use app\admin\logic\UserLogic;
use app\BaseController;

use app\model\Wx;
use app\service\ImgService;
use app\service\WxService;
use think\App;
use think\facade\Request;

class CleaningPoint extends BaseController
{

    public function __construct()
    {
//        logend(config('user'));
//        if(config('user')['position'] != \app\model\User::SYS_ADMIN)   fail('暂无权限');
    }

    /*
     * 列表
     */
    public function index()
    {
        $logic = new CleaningPointLogic();
        success($logic->getList());
    }
    /*
     * 添加组织
     * 默认添加一个组织管理员
     */
    public function create()
    {
        $logic = new CleaningPointLogic();
        success($logic->addPoint());
    }

    /*
     * 详情
     */
    public function read()
    {
        $param = Request::get();
        if(!isset($param['cleaningPointId']) || empty($param['cleaningPointId']))   fail('请选择保洁点');
        $pointId = intval($param['cleaningPointId']);

        $pointModel = new \app\model\CleaningPoint();

        $info = $pointModel->getPointInfoById($pointId);
        if(!$info)  fail('保洁点信息不存在');

        success($info);

    }

    public function update()
    {
        $logic = new CleaningPointLogic();
        success($logic->updatePoint());
    }

    /*
     * 启用或者禁用
     */
    public function del()
    {
        $param = Request::delete();
        if(!isset($param['cleaningPointId']) || empty($param['cleaningPointId']))   fail('请选择保洁点');
        $id = intval($param['cleaningPointId']);

        $logic = new CleaningPointLogic();
        success($logic->del($id));
    }

    public function cleaningQrcode()
    {

        $param = Request::get();
        if(!isset($param['cleaningPointId']) || empty($param['cleaningPointId']))   fail('请选择保洁点');
        $id = intval($param['cleaningPointId']);

        $pointModel = new \app\model\CleaningPoint();
        $info = $pointModel->getPointInfoById($id);
        if(!$info)  fail('请选择正确的保洁点');

        if(isset($info['qrcode']) && !empty($info['qrcode'])){
            success($info['qrcode']);
        }
        $wxService = new WxService();
        $rt = $wxService->getwxacodeunlimit('pages/clean/clean','?orgid='.$info['orgid'].'&cleaningPointId='.$id,789);
        if($rt){
            //保存保洁点二维码

            $point['cp_qrcode'] = fileFullPath($rt);
            $pointModel->updatePointInfoByWhere(['cp_id'=>$id],$point);
            success(fileFullPath($rt));
        }
        fail('二维码获取失败');
    }

    public function inspectionQrcode()
    {

        $param = Request::get();
        if(!isset($param['cleaningPointId']) || empty($param['cleaningPointId']))   fail('请选择保洁点');
        $id = intval($param['cleaningPointId']);

        $pointModel = new \app\model\CleaningPoint();
        $info = $pointModel->getPointInfoById($id);
        if(!$info)  fail('请选择正确的保洁点');

        if(isset($info['checkQrcode']) && !empty($info['checkQrcode'])){
            success($info['checkQrcode']);
        }
        $wxService = new WxService();
        $rt = $wxService->getwxacodeunlimit('pages/inspection/inspection','?orgid='.$info['orgid'].'&cleaningPointId='.$id,789,2);
        if($rt){
            //保存保洁点二维码

            $point['cp_check_qrcode'] = fileFullPath($rt);
            $pointModel->updatePointInfoByWhere(['cp_id'=>$id],$point);
            success(fileFullPath($rt));
        }
        fail('二维码获取失败');
    }

}